local more1__dangxian = fk.CreateSkill{
  name = "more1__dangxian",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["more1__dangxian"] = "当先",
  [":more1__dangxian"] = "锁定技，回合开始时，你执行一个额外的摸牌阶段和出埤阶段。"..
  "摸牌阶段或出牌阶段开始时，你从牌堆中获得一张不计入手牌上限的【杀】并记录之："..
  "结束阶段，若你有“当先“记录过的【杀】，弃置之并失去等量点体力，然后视为使用一张不可响应的伤害类锦囊。",
  ["@@more1__dangxian-inhand"] = "当先",
  ["@more1__dangxian"] = "当先" ,
  ["#more1__dangxian-choose"] = "选择一张伤害型锦囊",
  ["#more1__dangxian-damage"] = "选择使用",
  ["$more1__dangxian1"] = "谁说蜀中已无大将!",
  ["$more1__dangxian2"] = "吾虽年迈，宝剑尚锋!",
}

more1__dangxian:addEffect(fk.TurnStart, {
  anim_type = "offensive",
  on_use = function(self, event, target, player, data)

    player:gainAnExtraPhase(Player.Play, more1__dangxian.name)
    player:gainAnExtraPhase(Player.Draw, more1__dangxian.name)
  end,
})
more1__dangxian:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(more1__dangxian.name, true) and (target.phase == Player.Draw or target.phase == Player.Play)
  end,
  on_use = function(self, event, target, player, data)
    local cards = player.room:getCardsFromPileByRule("slash", 1, "drawPile")
    if #cards > 0 then
      player.room:obtainCard(player, cards[1], true, fk.ReasonJustMove, player, more1__dangxian.name)
    end
  end,
})
more1__dangxian:addEffect(fk.AfterCardsMove, {
  mute = true,

  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(more1__dangxian.name) and player.room.current == player then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand and move.skillName == more1__dangxian.name then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Player.Hand and move.skillName == more1__dangxian.name then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(player:getCardIds("h"), info.cardId) then
            room:setCardMark(Fk:getCardById(info.cardId), "@@more1__dangxian-inhand", 1)
            room:addPlayerMark(player, "@more1__dangxian", 1)
          end
        end
      end
    end
  end,
})
more1__dangxian:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(more1__dangxian.name) and player.phase == Player.Finish then
      if player:isKongcheng() then return end
      local cards = player:getCardIds("h")
      local n = #table.filter(cards, function(id)
        return Fk:getCardById(id):getMark("@@more1__dangxian-inhand") > 0
      end)
      return n > 0 
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local discard = {}
    for _, p in ipairs(player:getCardIds("h")) do
      if Fk:getCardById(p):getMark("@@more1__dangxian-inhand") > 0 then
        table.insert(discard,p)
      end
    end
    room:loseHp(player, #discard, more1__dangxian.name)
    room:throwCard(discard, more1__dangxian.name, player, player)
    local names = {}
    for name, _ in pairs(Fk.all_card_types) do
      local card = Fk:cloneCard(name)
      if not card.is_derived and card.is_damage_card and card.type == Card.TypeTrick then  
        table.insertIfNeed(names, name)
      end
    end
    if #names > 0 then 
      local choice = room:askToChoice(player, {
        choices = names,
        skill_name = more1__dangxian.name,
        prompt = "#more1__dangxian-choose",
        cancelable = false,
      })
      if #choice > 0 then
        local use = room:askToUseVirtualCard(player, {
          name = choice,
          skill_name = more1__dangxian.name,
          prompt = "#more1__dangxian-damage",
          extra_data = {
            bypass_times = true,
            extraUse = true,
          },
          cancelable = false,
        })
      end
    end
  end,
})

more1__dangxian:addEffect(fk.TargetSpecified, {
  can_trigger = function(self, event, target, player, data)
    return table.contains(data.card.skillNames, more1__dangxian.name) and player:hasSkill(more1__dangxian.name)
  end,
  on_use = function(self, event, target, player, data) 
    data.disresponsive = true
  end,
})


more1__dangxian:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return card:getMark("@@more1__dangxian-inhand") > 0
  end,
})




return more1__dangxian
